package com.ywt.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.ywt.model.AreasModel;
import com.ywt.model.ChannelsModel;
import com.ywt.model.ConfigModel;

public class DbTool {

	private ConDbHelper dbHelper = null;

	public DbTool(Context context) {
		dbHelper = new ConDbHelper(context);
		dbHelper.open();
	}

	public void close() {
		if (dbHelper != null) {
			dbHelper.close();
			dbHelper = null;
		}

	}

	public ConfigModel getConfig() {
		ConfigModel cm = null;
		Cursor c = dbHelper.fetchAllConfig();
		System.out.println("Here3 >>>>>> " + String.valueOf(c));
		if (c.getCount() > 0) {

			  cm = new ConfigModel();
			cm.setArea_code(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_area_code)));
			cm.setChannel_code(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_channel_code)));
			cm.setNewstype_code(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_newstype_code)));
			cm.setNewtype_code(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_newtype_code)));
			cm.setVER_CODE(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_VER_CODE)));
			cm.setVER_PATH(c.getString(c
					.getColumnIndexOrThrow(ConDbHelper.KEY_VER_PATH)));

		}
		return cm;

	}

	public void cleanAllAreas() {
		dbHelper.clearAllAreas();
	}

	public void cleanAllChannels() {
		dbHelper.clearAllChannels();
	}

	public ArrayList<AreasModel> getAllAreas() {
		ArrayList<AreasModel> list = null;
		Cursor c = dbHelper.fetchAllAreas();
		if (c.getCount() > 0) {
			list = new ArrayList<AreasModel>();
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				AreasModel cm = new AreasModel();
				cm.setArea_ID(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Area_ID)));
				cm.setArea_Name(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Area_Name)));
				cm.setArea_Code(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Area_Code)));
				cm.setParent_ID(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Parent_ID)));
				list.add(cm);
			}
		}
		return list;
	}

	public ArrayList<ChannelsModel> getAllChannels() {

		ArrayList<ChannelsModel> list = null;

		Cursor c = dbHelper.fetchAllChannels();
		if (c.getCount() > 0) {
			list = new ArrayList<ChannelsModel>();
			for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
				ChannelsModel cm = new ChannelsModel();

				cm.setChannel_ID(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_ID)));
				cm.setChannel_Name(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_Name)));
				cm.setChannel_Price(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_Price)));
				cm.setChannel_Unit(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_Unit)));
				cm.setChannel_Status(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_Status)));
				cm.setDealLine_Date(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_DealLine_Date)));
				cm.setChannel_Order(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Channel_Order)));
				cm.setOperator_ID(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Operator_ID)));
				cm.setCreate_Date(c.getString(c
						.getColumnIndexOrThrow(ConDbHelper.KEY_Create_Date)));
				list.add(cm);
			}
		}
		return list;
	}

	public long insertConfig(String area_code, String channel_code,
			String newstype_code, String newtype_code, String version_code,
			String version_path) {
		return dbHelper.insertConfig(area_code, channel_code, newstype_code,
				newtype_code, version_code, version_path);

	}

	public long insertAreas(String Area_ID, String Area_Name, String Area_Code,
			String Parent_ID) {
		return dbHelper.insertAreas(Area_ID, Area_Name, Area_Code, Parent_ID);

	}

	public long insertChannels(String Channel_ID, String Channel_Name,
			String Channel_Price, String Channel_Unit, String Channel_Status,
			String DealLine_Date, String Channel_Order, String Operator_ID,
			String Create_Date) {
		return dbHelper.insertChannels(Channel_ID, Channel_Name, Channel_Price,
				Channel_Unit, Channel_Status, DealLine_Date, Channel_Order,
				Operator_ID, Create_Date);

	}
}
